/**
 * 用户和公众号聊天消息
 *
 * 暂不支持news(图文格式,但不在素材库中)
 * @authors 杨兴洲（of2502）
 * @date    2017/4/26 0026 11:36
 * @version 1.0
 */

import {model, Schema} from "mongoose";
const Message = new Schema({
    toUser: {"type": String, "required": true},// 公主号id/粉丝id
    fromUser: {"type": String, "required": true},
    msgType: {"type": String, "required": true},// 消息类型:text/voice/mpnews/image/mpvideo/wxcard
    text: {
        content: String
    },
    voice: {
        media_id: String,
        format: String // amr
    },
    mpnews: {
        media_id: String
    },
    music: {
        title: String,
        description: String,
        musicurl: String,
        hqmusicurl: String,
        thumb_media_id: String
    },
    image: {
        media_id: String,
        pic_url: String
    },
    video: {
        media_id: String,
        thumb_media_id: String,
        title: String,
        description: String
    },
    wxcard: {
        card_id: String
    },
    // isReply: {"type": Boolean, "default": false}, // 是否回复
    isStar: {"type": Boolean, "default": false}, // 是否收藏
    replyList: [{"type": Schema.Types.ObjectId, ref: "Message"}]// 回复列表（存在群发）

}, {
    timestamps: true,
    toJSON: {virtuals: true}
});
Message.virtual("isReply").get(function () {
    return this.replyList.length > 0;
});
Message.index({msgID: 1});
Message.index({toUser: 1, msgtype: 1},{ expireAfterSeconds: 3600 * 24 * 30});

export default model('Message', Message);
